<?php

require_once('../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/itusurveyplugin/lib.php');
require_once($CFG->dirroot . "/local/itusurveyplugin/forms.php");

//Require authentication and authorization
require_login($SITE);
require_capability('local/itusurveyplugin:doitusurvey', context_system::instance());

//prepare moodle_url
global $DB;
$path = $CFG->wwwroot.'/local/itusurveyplugin/itusurveyfilling/dosurvey.php';

$itus_id = required_param('itus', PARAM_TEXT);
$subject_id = required_param('sid', PARAM_TEXT);


/* Chuan bi trang web can hien thi */	
$url = new moodle_url($path);
$PAGE->set_url($url);
$PAGE->set_title('ITU Survey');
$PAGE->set_heading('ITU Survey');
$PAGE->set_pagelayout('standard');

navigation_node::override_active_url(new moodle_url($path, array('param'=>'value')));

echo $OUTPUT->header();
//Include functions
$mylib = new survey_management();
//New form instance
$mform = new form_do_survey(null, array("itus"=>$itus_id,"sid"=>$subject_id));

//cancel action
if ($mform->is_cancelled()) {

echo 'hủy thao tác';

}
else if ($fromform = $mform->get_data()) 
{
    //Update status of Combination -teacher-survey-subject to doing if it is not-opened
    $sts_id = $_POST["sts_id"];
        $DB->update_record("itus_teachers_subjects",array("id"=>$sts_id,"sts_status"=>2));
        echo "<script>redirectTo('notification.php')</script>";
}
else{
    $expiration = $mylib->isSurveyExpired($itus_id);
    if($expiration)
        echo "<script>redirectTo('notification.php')</script>";
}
$mform->display();
echo $OUTPUT->footer();

?>